package RSA;

import java.math.BigInteger;
import java.util.Scanner;

public class RSA {
    public static void main(String[] args) {
        //生成大质数p,q
        BigInteger p = MillerRabinPrime.generatePrime(128);
        BigInteger q = MillerRabinPrime.generatePrime(128);
        System.out.println("生成大素数:");
        System.out.println("p:"+p);
        System.out.println("q:"+q);
        //计算n和phi(n)
        BigInteger n = p.multiply(q);
        BigInteger phi = p.subtract(BigInteger.ONE).multiply(q.subtract(BigInteger.ONE));
        System.out.println("n:"+n);
        //选择e,确保e与phi(n)互质
        BigInteger e = new BigInteger("65537");
        System.out.println("公钥为:"+e);
        //计算d
        BigInteger d = e.modInverse(phi);
        System.out.println("私钥为:"+d);
//        Scanner scanner = new Scanner(System.in);
        BigInteger m = MillerRabinPrime.generatePrime(128);
        System.out.println("大整数生成的明文:"+m);

        //加密
        BigInteger cipherText = m.modPow(e, n);
        System.out.println("加密后的密文:" + cipherText);
        //解密
        BigInteger message = cipherText.modPow(d, n);
        System.out.println("解密结果："+message);
    }
}